<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>文章列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="stylesheet" href="/themes/admin/css/font.css">
    <link rel="stylesheet" href="/themes/admin/css/xadmin.css">
    <style type="text/css">
        .toolbar {
            border: 1px solid #ccc;
        }
        .text {
            border: 1px solid #ccc;
            min-height: 200px;
        }
    </style>
</head>

<body>
<div class="x-nav">
    <a class="layui-btn layui-btn-small layui-btn-normal" id="btn-back-article" style="float:right;margin-top: 4px"  title="返回">返回文章列表</a>
</div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <form class="layui-form" action="">
                        <div class="layui-form-item">
                            <label class="layui-form-label">标题</label>
                            <div class="layui-input-inline">
                                <input type="text" value="{$postDetail.title}" id="post-title" required  lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
                            </div>
                            <div class="layui-form-mid layui-word-aux">*</div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">正文</label>
                            <div class="layui-input-block">
                                <div id="toolbar" class="toolbar">
                                </div>
                                <div style="padding: 5px 0; color: #ccc"></div>
                                <div id="editor" class="text"></div>
                            </div>
                            <div class="layui-form-mid layui-word-aux">*</div>
                        </div>
                        <div class="layui-form-item" style="position: absolute;z-index: 9999999;bottom: 100px">
                            <label class="layui-form-label">分类</label>
                            <div class="layui-input-inline">
                                <select id="post-cate" lay-verify="required">
                                    <option value=""></option>
                                    {volist name="category" id="vo"}
                                    <option value="{$vo.cate_id}">{$vo.cate_name}</option>
                                    {/volist}
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">状态</label>
                            <div class="layui-input-block">
                                <input type="checkbox" id="post-status" lay-skin="switch" lay-text="公开|私密">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button type="button" class="layui-btn" onclick="postAdd()">提交</button>
                                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript" src="https://unpkg.com/wangeditor@3.1.1/release/wangEditor.min.js"></script>
<script src="/themes/admin/lib/layui/layui.js" charset="utf-8"></script>
<script type="text/javascript" src="/themes/admin/js/xadmin.js"></script>
<script>
    var page = 1
    if (localStorage.getItem("page") != null){
        page = localStorage.getItem("page");
    }
    $("#btn-back-article").attr("href","/admin/article/postList?page=" + page)

    var E = window.wangEditor
    var editor = new E('#toolbar', '#editor')
    editor.customConfig.uploadFileName = 'Image[]'
    editor.customConfig.uploadImgServer = '/admin/index/upload'
    // 将图片大小限制为 3M
    editor.customConfig.uploadImgMaxSize = 3 * 1024 * 1024
    // 限制一次最多上传 9 张图片
    editor.customConfig.uploadImgMaxLength = 9
    editor.customConfig.uploadImgHooks = {
        customInsert: function (insertImg, result, editor) {
            var urls = result.url
            $.each(urls,function (index,url) {
                insertImg(url)
            })
        }

    }
    editor.create()
layui.use(['form','layer'],function () {
    //初始化内容
    editor.txt.html('{$postDetail.content|raw}');
    var form = layui.form;
    var cateId = "{$postDetail.cate_id|default=''}";

    if (cateId != ""){
        var select = 'dd[lay-value=' + cateId + ']';
        $("#post-cate").siblings("div.layui-form-select").find('dl').find(select).click();
    }

    if ("{$postDetail.status}" == "公开"){
        $("#post-status").attr("checked", "checked");
    }else if ("{$postDetail.status}" == "私密") {
        $("#post-status").removeAttr("checked");
    }
    form.render();
});

function postAdd(){
    var postTitle = $("#post-title").val();
    var postContent = editor.txt.html();
    var postCate = $("#post-cate").val();
    var postStatus = 1;
    if ($('#post-status').is(':checked')) {
          postStatus = 0;
         }
    var postData = {"title":postTitle,"content":postContent,"cate_id":postCate,"status":postStatus,"id":"{$Request.get.id}"};

    $.ajax({
        type:"POST",
        data:postData,
        url:"/admin/article/postAdd",
        success:function (e) {
            if (e.code == 1){
                layer.msg(e.msg,{"icon":1})
                setTimeout(function () {
                    location.href = "/admin/article/postList";
                },1500)
            }else {
                layer.msg(e.msg,{"icon":2})
            }
        }
    })


}

</script>
</html>